proxy-bind
tl;dr
from
const fn = foobar.fn.bind(foobar);
to
const { fn } = bind(foobar);
Install
npm install proxy-bind
Import
import { bind, bond } from 'proxy-bind';
.
import { bind, bond } from 'https://deno.land/x/proxy_bind/mod.ts';
.
const { bind, bond } = require('proxy-bind');
or
<script type="module">
import { bind, bond } from 'https://cdn.jsdelivr.net/npm/proxy-bind@1.x/index.mjs';
</script>
.
<script type="javascript">
(async () => {
const { bind, bond } = await import('https://cdn.jsdelivr.net/npm/proxy-bind@1.x/index.mjs');
})();
</script>
the bond
is only an alias to avoid naming conflict, just in case.
Example
"point-free" ish
import { bind } from 'proxy-bind';
const { push, join } = bind([ 1, 2, 3 ]);
push(4);
console.log(join('-'));
import { bind } from 'proxy-bind';
const { resolve } = bind(Promise);
console.log(await resolve(42));
console.log(await resolve('foobar'));
bind this
import { bind } from 'proxy-bind';
const me = {
name: 'foo',
greet (you) {
return `Hi ${ you }, this is ${ this.name }.`;
},
};
const { greet } = bind(me);
console.log(greet('bar'));
mirror
helper
import { mirror } from 'proxy-bind';
const [ list, { push } ] = mirror([ 1, 2, 3 ]);
push(4);
console.log(list);
Methodology
It uses ES6 Proxy
to bridge function calls via Function.prototype.bind
.
Caveat
- Doesn't work on primitive types:
- don't
bind(5)
- do
bind(new Number(5))
License
the MIT